package com.demo.app.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.demo.app.R;
import com.demo.framework.ui.BaseActivity;
import com.demo.framework.widget.SignView;
import com.demo.framework.widget.SignView.OnNewTouchListener;

/**
 * 
 * @ClassName: SignActivity
 * @Description: TODO(用户签名签名)
 * @author 谢云腾
 * @date 2013-3-22 下午02:53:12
 * @version 1.0
 */
public class SignActivity extends BaseActivity implements OnNewTouchListener {

    SignView dp;

    private LinearLayout ll_sign;

    String user_phone = "";

    private TextView totalTxt;

    private TextView text_hint;

    private String photoName = null;
    
    public  SignActivity(){
        super(true,false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        setContentView(R.layout.sign_test_activity);

        ll_sign = (LinearLayout) findViewById(R.id.ll_sign);
        text_hint = (TextView) findViewById(R.id.text_hint);

        dp = new SignView(this);
        dp.setOnNewTouchListener(this);
        ll_sign.addView(dp);

    }

    private void completeBtnClick() {
        try {
            Bitmap signBitmap = getViewBitmap(dp);
            if (saveSignView(signBitmap, photoName + ".jpg")) {
                // save data
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    private Bitmap getViewBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.destroyDrawingCache();
        return bitmap;
    }

    private boolean saveSignView(Bitmap signBitmap, String filename) throws Exception {
        boolean result = false;
        String fileUrl = Environment.getExternalStorageDirectory().toString() + "/crm/" + filename;
        // File file = new File(fileUrl);
        // file.getParentFile().mkdirs();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(fileUrl));
            if (null != fos) {
                result = signBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
            }
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
                fos = null;
            }
            if (signBitmap != null) {
                signBitmap.recycle();
                signBitmap = null;
            }

        }
        return result;
    }

    @Override
    public void onTouch() {
        // TODO Auto-generated method stub
        text_hint.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        try {
            dp.clearData();
        }
        catch(Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    protected void setupView() {
        // TODO Auto-generated method stub
        
    }

}
